package top.zywork.controller;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import top.zywork.common.BeanUtils;
import top.zywork.common.RedisUtils;
import top.zywork.dto.DataDictAndItemDTO;
import top.zywork.dto.PagerDTO;
import top.zywork.query.DataDictAndItemQuery;
import top.zywork.service.DataDictAndItemService;
import top.zywork.vo.DataDictAndItemVO;
import top.zywork.vo.PagerVO;
import top.zywork.vo.ResponseStatusVO;

/**
 * DataDictAndItemController控制器类<br/>
 *
 * 创建于2019-11-29<br/>
 *
 * @author http://zywork.top 王振宇
 * @version 1.0
 */
@RestController
@RequestMapping("/data-dict-and-item")
@Slf4j
public class DataDictAndItemController extends BaseController {

    private static final String DATA_DICT_AND_ITEM_PREFIX = "data_dict_and_item::";

    private DataDictAndItemService dataDictAndItemService;

    private RedisTemplate<String, Object> redisTemplate;

    @GetMapping("admin/multi/{id}")
    public ResponseStatusVO listById(@PathVariable("id") Long id) {
        PagerDTO pagerDTO = dataDictAndItemService.listById(id);
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), DataDictAndItemVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    @GetMapping("admin/all")
    public ResponseStatusVO listAll() {
        PagerDTO pagerDTO = dataDictAndItemService.listAll();
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), DataDictAndItemVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    @PostMapping("admin/all-cond")
    public ResponseStatusVO listAllByCondition(@RequestBody DataDictAndItemQuery dataDictAndItemQuery) {
        PagerDTO pagerDTO = dataDictAndItemService.listAllByCondition(dataDictAndItemQuery);
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), DataDictAndItemVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    /**
     * 根据指定的多个数据字典的code获取多个数据字典项的值，并封装成指定的json格式：
     * {
     *                     IS_ACTIVE: [
     *                         {
     *                             value: 0,
     *                             displayValue: '激活',
     *                             isDefault: 1
     *                         },
     *                         {
     *                             value: 1,
     *                             displayValue: '冻结',
     *                             isDefault: 0
     *                         }
     *                     ]
     *                 }
     * @param dictCodes
     * @return
     */
    @PostMapping("any/by-dict-codes")
    public ResponseStatusVO anyListByDictCodes(@RequestBody(required = false) String[] dictCodes) {
        JSONObject returnJsonObj = new JSONObject();
        if (dictCodes != null) {
            for (String dictCode : dictCodes) {
                DataDictAndItemQuery dataDictAndItemQuery = DataDictAndItemQuery.builder().dataDictDictCode(dictCode).dataDictItemIsActive((byte) 0).build();
                dataDictAndItemQuery.setSortColumn("dataDictItemDictOrder");
                dataDictAndItemQuery.setSortOrder("asc");
                PagerDTO pagerDTO = dataDictAndItemService.listAllByCondition(dataDictAndItemQuery);
                JSONObject[] jsonObjects = new JSONObject[pagerDTO.getTotal().intValue()];
                String key = null;
                for (int i = 0, len = pagerDTO.getTotal().intValue(); i < len; i++) {
                    DataDictAndItemDTO dataDictAndItemDTO = (DataDictAndItemDTO) pagerDTO.getRows().get(i);
                    if (key == null) {
                        key = dataDictAndItemDTO.getDataDictDictCode();
                    }
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("value", dataDictAndItemDTO.getDataDictItemDictValue());
                    jsonObject.put("displayValue", dataDictAndItemDTO.getDataDictItemDictDisplayValue());
                    jsonObject.put("color", dataDictAndItemDTO.getDataDictItemColor());
                    jsonObject.put("isDefault", dataDictAndItemDTO.getDataDictItemIsDefault());
                    jsonObjects[i] = jsonObject;
                }
                if (key != null) {
                    returnJsonObj.put(key, jsonObjects);
                }
            }
        }
        return ResponseStatusVO.ok("查询成功", returnJsonObj);
    }

    /**
     * 根据code删除数据字典缓存
     * @param dictCode
     * @return
     */
    @GetMapping("any/clear-cache/{dictCode}")
    public ResponseStatusVO anyClearCache(@PathVariable("dictCode") String dictCode) {
        RedisUtils.delete(redisTemplate, DATA_DICT_AND_ITEM_PREFIX + dictCode);
        return ResponseStatusVO.ok("删除数据字典缓存成功", null);
    }

    @PostMapping("admin/pager-cond")
    public ResponseStatusVO listPageByCondition(@RequestBody DataDictAndItemQuery dataDictAndItemQuery) {
        PagerDTO pagerDTO = dataDictAndItemService.listPageByCondition(dataDictAndItemQuery);
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), DataDictAndItemVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    @Autowired
    public void setDataDictAndItemService(DataDictAndItemService dataDictAndItemService) {
        this.dataDictAndItemService = dataDictAndItemService;
    }

    @Autowired
    public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }
}
